package com.ly.heart.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.MaterialTypeEntity;
import com.ly.heart.mapper.MaterialTypeMapper;
import com.ly.heart.service.IMaterialTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 物料分类Service业务层处理
 * 
 * @author wutao
 * @date 2024-05-24
 */
@Service
public class MaterialTypeServiceImpl extends ServiceImpl<MaterialTypeMapper, MaterialTypeEntity> implements IMaterialTypeService
{
    @Resource
    private MaterialTypeMapper erpMaterialTypeMapper;

    /**
     * 查询物料分类
     * 
     * @param id 物料分类ID
     * @return 物料分类
     */
    @Override
    public MaterialTypeEntity selectErpMaterialTypeById(String id)
    {
        return erpMaterialTypeMapper.selectErpMaterialTypeById(id);
    }

    /**
     * 查询物料分类列表
     * 
     * @param erpMaterialType 物料分类
     * @return 物料分类
     */
    @Override
    public List<MaterialTypeEntity> selectErpMaterialTypeList(MaterialTypeEntity erpMaterialType)
    {
        List<MaterialTypeEntity> list = erpMaterialTypeMapper.selectErpMaterialTypeList(erpMaterialType);
        //获取一级目录
        List<MaterialTypeEntity> parentList = list.stream().filter(s -> "0".equals(s.getParentId())).collect(Collectors.toList());
        for (MaterialTypeEntity m : parentList){
            m.setTreeList(getChilde(m.getId(),list));
        }
        return parentList;
    }

    /**
     * 新增物料分类
     * 
     * @param erpMaterialType 物料分类
     * @return 结果
     */
    @Override
    public int insertErpMaterialType(MaterialTypeEntity erpMaterialType)
    {
        erpMaterialType.setId(SnowflakeIdWorker.getInstance().nextStringId());
        erpMaterialType.setCreateTime(DateUtils.getNowDate());
        return erpMaterialTypeMapper.insertErpMaterialType(erpMaterialType);
    }

    /**
     * 修改物料分类
     * 
     * @param erpMaterialType 物料分类
     * @return 结果
     */
    @Override
    public int updateErpMaterialType(MaterialTypeEntity erpMaterialType)
    {
        erpMaterialType.setUpdateTime(DateUtils.getNowDate());
        return erpMaterialTypeMapper.updateErpMaterialType(erpMaterialType);
    }

    /**
     * 批量删除物料分类
     * 
     * @param ids 需要删除的物料分类ID
     * @return 结果
     */
    @Override
    public int deleteErpMaterialTypeByIds(String[] ids)
    {
        return erpMaterialTypeMapper.deleteErpMaterialTypeByIds(ids);
    }

    /**
     * 删除物料分类信息
     * 
     * @param id 物料分类ID
     * @return 结果
     */
    @Override
    public int deleteErpMaterialTypeById(String id)
    {
        return erpMaterialTypeMapper.deleteErpMaterialTypeById(id);
    }



    /**
     * 递归查找子菜单
     *
     * @param id 当前菜单id
     * @param rootList 要查找的列表
     * @return
     */
    private List<MaterialTypeEntity> getChilde(String id, List<MaterialTypeEntity> rootList){
        //子级
        List<MaterialTypeEntity> childList = new ArrayList<>();
        for(MaterialTypeEntity menuInfo : rootList){
            // 遍历所有节点，将父级id与传过来的id比较
            if(menuInfo.getParentId().equals(id)){
                childList.add(menuInfo);
            }
        }
        // 把子级的子级再循环一遍
        for(MaterialTypeEntity sonMenuInfo : childList){
            sonMenuInfo.setTreeList(getChilde(sonMenuInfo.getId(), rootList));
        }
        // 递归退出条件
        if (childList.isEmpty()){
            return null;
        }
        return childList;
    }
}
